
package com.vdong.common.oauth.common.authorize;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/**
 * @author 刀锋
 * @date : Create in 9:52 2018/6/12
 * @description:
 */

@Configuration
@EnableWebSecurity
@EnableRedisHttpSession
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class AuthConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AuthAuthenticationProvider authAuthenticationProvider;
    @Autowired
    private AuthAuthenticationDetailsSource authAuthenticationDetailsSource;
    @Autowired
    private AuthAuthenticationFailureHandler authAuthenticationFailureHandler;
    @Autowired
    private AuthAuthenticationSuccessHandler authAuthenticationSuccessHandler;
    @Autowired
    private AuthAccessDeniedHandler authAccessDeniedHandler;
    @Autowired
    private AuthAuthenticationEntryPoint authAuthenticationEntryPoint;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .exceptionHandling()
                .accessDeniedHandler(authAccessDeniedHandler)
                .authenticationEntryPoint(authAuthenticationEntryPoint)
                .and()
                .csrf().disable()
                .cors().disable()
                .authorizeRequests()
                .antMatchers("/login/execute").permitAll()
                .antMatchers("/login2").permitAll()
                .antMatchers("/notify").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .successHandler(authAuthenticationSuccessHandler)
                .failureHandler(authAuthenticationFailureHandler)
                .permitAll()
                .authenticationDetailsSource(authAuthenticationDetailsSource)
                .and()
                .logout()
                .logoutUrl("/logout");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        this.authAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        this.authAuthenticationProvider.setHideUserNotFoundExceptions(false);
        auth.authenticationProvider(this.authAuthenticationProvider);

    }

    @Override
    public void configure(WebSecurity websecurity) throws Exception {
        websecurity.ignoring().antMatchers("/static/**");
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(6);
        return bCryptPasswordEncoder;
    }
}

